<?php
	session_start();
	require_once("db_connect.php");

	$thread_name = htmlspecialchars($_POST['thread_name'],ENT_QUOTES);
	$thread_data = htmlspecialchars($_POST['thread_data'],ENT_QUOTES);
	$thread_id = 0;

    $sql = "INSERT INTO thread(thread_name) VALUES(?)";
    $stmt = $db->prepare($sql);

	$db->execute($stmt, $thread_name);
    if (DB::isError($db)) {
        exit($res->getMessage());
    }

    $sql = "SELECT thread_id from thread where thread_name = ?";
	$sth = $db->query($sql,$thread_name);
	if (DB::isError($sth)) {
	  exit($sth->getMessage());
	}

	while ($data = $sth->fetchRow(DB_FETCHMODE_ASSOC)) {
		$thread_id = $data['thread_id'];
	}


    $sql = "INSERT INTO response(response_id,user_id,thread_id,response_data) VALUES (NULL,?,?,?)";
    $stmt = $db->prepare($sql);

    $ary = array($_SESSION['user_id'],$thread_id,$thread_data);
	$db->execute($stmt, $ary);
    if (DB::isError($db)) {
        exit($res->getMessage());
    }

    header("Location: response.php?thread_id=".$thread_id);
